﻿using SHKF.B2C.JuShuiTan.Model.Entity;
using SqlSugar;
using System;

namespace SHKF.B2C.JuShuiTan.BLL
{
    public class TokenBLL : CenterBLL
    {
        public TokenBLL(SqlSugarClient db) :base(db)
        {

        }

        public TokenEntity Get(string CompanyID)
        {
            TokenEntity tokenEntity;
            if (string.IsNullOrEmpty(CompanyID))
            {
                tokenEntity = this.db
               .Queryable<TokenEntity>() 
               .First();
            }
            else
            {
                tokenEntity = this.db
               .Queryable<TokenEntity>()
               .Where(ww => ww.id == CompanyID)
               .First(); 
            }
            return tokenEntity;
        }

        public TokenEntity Save(TokenEntity info)
        {
            TokenEntity old = this.db
                .Queryable<TokenEntity>()
                .Where(ww => ww.CompanyName == info.CompanyName)
                .First();

            if(old != null)
            {
                info.SN = old.SN;
                this.db.Updateable<TokenEntity>(info).ExecuteCommand();
            }
            else
            {
                long SN = this.db
               .Insertable<TokenEntity>(info)
               .ExecuteReturnBigIdentity();

                info.SN = SN;
            } 
            return info;
        }
    }
}
